package com.yl.module.property.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.yl.common.constant.Constants;
import com.yl.common.core.domain.entity.SysUser;
import com.yl.common.exception.ServiceException;
import com.yl.common.utils.NumberUtils;
import com.yl.framework.interceptor.UserContext;
import com.yl.module.file.domain.FileType;
import com.yl.module.file.domain.vo.File2VO;
import com.yl.module.file.service.FileService;
import com.yl.module.property.domain.Property;
import com.yl.module.property.domain.PropertyUpkeep;
import com.yl.module.property.domain.dto.PropertyChangeAddOrUpdDTO;
import com.yl.module.property.domain.dto.PropertyEnableIdleDTO;
import com.yl.module.property.mapper.PropertyChangeMapper;
import com.yl.module.property.service.PropertyChangeService;
import com.yl.module.property.service.PropertyService;
import com.yl.system.service.ISysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.yl.module.property.domain.vo.PropertyChangeVO;
import com.yl.module.property.domain.dto.PropertyChangeDTO;
import com.yl.module.property.domain.PropertyChange;
import cn.hutool.core.bean.BeanUtil;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 资产更换记录表 Service实现类
 * @author liangjian
 * @date 2024-10-27 21:10:48
 */
@Service
@Slf4j
public class PropertyChangeServiceImpl extends ServiceImpl<PropertyChangeMapper, PropertyChange> implements PropertyChangeService {

	@Autowired
	PropertyChangeMapper propertyChangeMapper;
	@Autowired
	FileService fileService;
	@Autowired
	ISysUserService sysUserService;
	@Autowired
	PropertyService propertyService;

   /**
	 * 查询所有
	 */
	@Override
	public List<PropertyChangeVO> getAll(){
		List<PropertyChange> list = this.list();
		List<PropertyChangeVO> result = BeanUtil.copyToList(list, PropertyChangeVO.class);
		return result;
	}

	/**
     * 分页查询
     */
	@Override
    public Page<PropertyChange> getPage(PropertyChangeDTO dto){
		//创建分页对象
		Page<PropertyChange> page = new Page(dto.getPageNum(), dto.getPageSize());
		//dto查询条件转分页条件
		LambdaQueryWrapper<PropertyChange> lqw = new LambdaQueryWrapper<>();
		Page<PropertyChange> result = propertyChangeMapper.selectPage(page, lqw);
		return result;
	}

	/**
	 * 通过主键查询
	 */
	@Override
	public PropertyChangeVO getByKey(Long id){
		PropertyChange entity = super.getById(id);
		PropertyChangeVO result = BeanUtil.copyProperties(entity, PropertyChangeVO.class);

		Map<Long, SysUser> userMap = sysUserService.getMapByIds(Lists.newArrayList(result.getUserId()));
		result.setNickName(sysUserService.getNameByMap(userMap,result.getUserId()));

		result.setProperty(propertyService.getById(result.getPropertyId()));
		if(!NumberUtils.integerEmpty(result.getPropertyIdLater())){
			result.setPropertyLater(propertyService.getById(result.getPropertyIdLater()));
		}

		List<File2VO> files = fileService.get(FileType.TypeEnum.FILE_TYPE_1003.getCode(), result.getId());
		result.setFiles(files);

		return result;
    }

	/**
	 * 添加
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public Long add(PropertyChangeAddOrUpdDTO dto){

		if(!NumberUtils.integerEmpty(dto.getPropertyIdLater())&&dto.getPropertyId().equals(dto.getPropertyIdLater())){
			throw new ServiceException("更换后的资产不能是原资产，请重新选择");
		}

		//保存更换记录
		PropertyChange propertyChange = BeanUtil.copyProperties(dto, PropertyChange.class);
		propertyChange.setUserId(UserContext.getUser().getUserId());
		this.save(propertyChange);

		//更换图片记录处理
		if(!CollectionUtils.isEmpty(dto.getFileIds())){
			fileService.addSave(propertyChange.getId(),dto.getFileIds());
		}

		//如果有选择更换后的资产 那么涉及到ueCode的逻辑才去执行 这样做的原因是 如果没有选择 以前的ueCode肯定不能直接置空 如果置空了 当他在选择更换后的资产 此时就没有ueCode了
		if(!NumberUtils.integerEmpty(dto.getPropertyIdLater())){
			//以前的资产ueCode设置为空
			Property property = propertyService.getById(dto.getPropertyId());
			Property propertyUpd = new Property();
			propertyUpd.setId(property.getId());
			propertyUpd.setBimCode("");
			propertyUpd.setUeCode("");
			propertyUpd.setUpdateTime(LocalDateTime.now());
			propertyService.updateById(propertyUpd);

			//把以前资产的ueCode设置给现在的资产
			Property propertyUpdLater = new Property();
			propertyUpdLater.setId(dto.getPropertyIdLater());
			propertyUpdLater.setBimCode(property.getBimCode());
			propertyUpdLater.setUeCode(property.getUeCode());
			propertyUpdLater.setIsIdle(Constants.NO);
			propertyUpdLater.setUpdateTime(LocalDateTime.now());
			propertyService.updateById(propertyUpdLater);
		}

		//更换后 原资产设置为闲置
		PropertyEnableIdleDTO propertyEnableIdleDTO = new PropertyEnableIdleDTO();
		propertyEnableIdleDTO.setId(dto.getPropertyId());
		propertyEnableIdleDTO.setIsIdle(Constants.YES);
		propertyService.enableIdle(propertyEnableIdleDTO);

		return propertyChange.getId();
	}

	/**
	 * 通过主键修改
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void upd(PropertyChangeAddOrUpdDTO dto){
		if(!NumberUtils.integerEmpty(dto.getPropertyIdLater())&&dto.getPropertyId().equals(dto.getPropertyIdLater())){
			throw new ServiceException("更换后的资产不能是原资产，请重新选择");
		}

		//更换记录的更换后资产是不能被修改的
		PropertyChange propertyChange = this.getById(dto.getId());
		if(!NumberUtils.integerEmpty(propertyChange.getPropertyIdLater())&&!propertyChange.getPropertyIdLater().equals(dto.getPropertyIdLater())){
			throw new ServiceException("如果已经添加了更换后的资产，那么更换后的资产是不能被修改的");
		}

		//修改保存记录
		PropertyChange propertyChangeUpd = BeanUtil.copyProperties(dto, PropertyChange.class);
		propertyChangeUpd.setUserId(UserContext.getUser().getUserId());
	    this.updateById(propertyChangeUpd);

		//更换记录文件处理
		fileService.updSave(FileType.TypeEnum.FILE_TYPE_1003.getCode(),propertyChangeUpd.getId(),dto.getFileIds());

		//如果有选择更换后的资产 那么涉及到ueCode的逻辑才去执行 这样做的原因是 如果没有选择 以前的ueCode肯定不能直接置空 如果置空了 当他在选择更换后的资产 此时就没有ueCode了
		//只有第一次填写了更换后的资产才会去执行
		if(NumberUtils.integerEmpty(propertyChange.getPropertyIdLater())&&!NumberUtils.integerEmpty(dto.getPropertyIdLater())){
			//以前的资产ueCode设置为空
			Property property = propertyService.getById(dto.getPropertyId());
			Property propertyUpd = new Property();
			propertyUpd.setId(property.getId());
			propertyUpd.setBimCode("");
			propertyUpd.setUeCode("");
			propertyUpd.setUpdateTime(LocalDateTime.now());
			propertyService.updateById(propertyUpd);

			//把以前资产的ueCode设置给现在的资产
			Property propertyUpdLater = new Property();
			propertyUpdLater.setId(dto.getPropertyIdLater());
			propertyUpdLater.setBimCode(property.getBimCode());
			propertyUpdLater.setUeCode(property.getUeCode());
			propertyUpdLater.setUpdateTime(LocalDateTime.now());
			propertyService.updateById(propertyUpdLater);
		}

	}

	@Override
	public void del(Long id){
		if(NumberUtils.longEmpty(id)){
			throw new ServiceException("参数id不能为空");
		}

		PropertyChange entity = new PropertyChange();
		entity.setId(id);
		entity.setDeleteFlag(Constants.YES);
		this.updateById(entity);
	}

}



